﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DatNuocViet.Models;
using System.Text.RegularExpressions;

namespace DatNuocViet.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewBag.nFeaturedTours = ToursDAO.getAmountOfFeaturedTours();
            ViewBag.listNewsType = NewsDAO.GetListNewsType();

            
            return View();
        }

        public ActionResult Partner()
        {
            List<GetToursImageResult> model = new List<GetToursImageResult>();
            using (DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
            {
                model = db.GetToursImage(0).ToList();
                ViewBag.listImage = model;
            }

            return View();
        }

        public ActionResult Intro()
        {
            WebResource rs = WebResourceDAO.GetWebResource("datnuocviet.aboutus");
            return View(rs);
        }

        public ActionResult Services()
        {
            WebResource rs = WebResourceDAO.GetWebResource("datnuocviet.service");
            return View(rs);
        }

        public ActionResult Event()
        {
            WebResource rs = WebResourceDAO.GetWebResource("datnuocviet.event");
            return View(rs);
        }

        [HttpPost]
        public bool setLanguage(string lang)
        {
            try
            {
                Session["Language"] = lang;
                return true;
            }
            catch
            {
                return false;
            }
        }

        public ActionResult FeaturedTours(int page = 1)
        {
            List<Tour> featuredTours = Models.ToursDAO.GetFeaturedTours(page, 3);

            return View(featuredTours);
        }

        public ActionResult listTours(int type)
        {
            return View(Models.ToursDAO.GetListToursIndex(type));
        }

        public ActionResult newsContent(int type)
        {
            ViewBag.NewsTypeName = NewsDAO.GetNewsType(type);
            return View(Models.NewsDAO.GetListNews(type, 7));
        }

        public ActionResult dulich360()
        {
            New news = NewsDAO.GetLastNews(1);
            return View(news);
        }

        public ActionResult videoclips()
        {
            New news = NewsDAO.GetLastNews(4);
            return View(news);
        }

        [HttpPost]
        public string SubscribeEmail(string Email)
        {
            Email = Email.Trim();
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(Email);
            if (match.Success)
            {
                try
                {
                    using(DatNuocVietDbDataContext db = new DatNuocVietDbDataContext())
                    {
                        if(db.CheckEmail(Email).Count() > 0)
                        {
                            return DatNuocViet.Ultility.Helper.Label("emailinvalid");
                        }
                        else
                        {
                            db.InsertEmail(Email);
                            return DatNuocViet.Ultility.Helper.Label("SubscribeEmailSuccess");
                        }
                    }
                }
                catch(Exception)
                {
                    return DatNuocViet.Ultility.Helper.Label("SubscribeEmailFail");
                }

            }
            else
            {
                return DatNuocViet.Ultility.Helper.Label("emailinvalid");
            }
        }
    }
}
